package st.business.tepth.business.history;

import android.graphics.Color;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import st.business.tepth.R;
import st.business.tepth.business.bean.CommonBean;
import st.business.tepth.config.ItemFields;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * @author zhangyu
 * @date 2019/2/23
 */
public class HistorySearchKeyAdapter extends BaseMultiItemQuickAdapter<MultipleItemEntity, BaseViewHolder> {
    public static final int TYPE_0 = 0;
    public static final int TYPE_1 = 1;
    public static final int TYPE_2 = 2;
    private List<MultipleItemEntity> mCheckedList = new ArrayList<>();

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public HistorySearchKeyAdapter(List<MultipleItemEntity> data) {
        super(data);
        addItemType(TYPE_0, R.layout.item_history_search_key_type_0);
        addItemType(TYPE_1, R.layout.item_history_search_key_type_1);
        addItemType(TYPE_2, R.layout.item_history_search_key_type_1);
    }


    @Override
    protected void convert(final BaseViewHolder helper, final MultipleItemEntity item) {
        switch (item.getItemType()) {
            case TYPE_0:
                helper.setText(R.id.tv_classification, (CharSequence) item.getField("name"));
                helper.setTextColor(R.id.tv_classification, Color.parseColor("#999999"));
                break;
            case TYPE_1:
                helper.setChecked(R.id.check_person, (Boolean) item.getField("checked"));
                helper.getView(R.id.layout_checkbox).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isChecked = item.getField("checked");
                        helper.setChecked(R.id.check_person, !isChecked);
                        item.setField("checked", !isChecked);
                        if (!isChecked) {
                            mCheckedList.add(item);
                        } else {
                            mCheckedList.remove(item);
                        }
                    }
                });
                CommonBean classificationBean = item.getField(ItemFields.ITEM_OBJ);
                helper.setText(R.id.check_person, classificationBean.getName());
                break;
            case TYPE_2:
                helper.setChecked(R.id.check_person, (Boolean) item.getField("checked"));
                helper.getView(R.id.layout_checkbox).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isChecked = item.getField("checked");
                        helper.setChecked(R.id.check_person, !isChecked);
                        item.setField("checked", !isChecked);
                        if (!isChecked) {
                            mCheckedList.add(item);
                        } else {
                            mCheckedList.remove(item);
                        }
                    }
                });
                CommonBean workTypeBean = item.getField(ItemFields.ITEM_OBJ);
                helper.setText(R.id.check_person, workTypeBean.getName());
                break;
        }


    }

    public String[] getSelectKeys() {
        String[] keys = new String[2];
        if (mCheckedList != null && !mCheckedList.isEmpty()) {
            StringBuilder classificationIds = new StringBuilder();
            StringBuilder typeIds = new StringBuilder();
            for (MultipleItemEntity entity : mCheckedList) {
                CommonBean commonBean = entity.getField(ItemFields.ITEM_OBJ);
                if (TYPE_1 == entity.getItemType()) {
                    //分系
                    classificationIds.append(commonBean.getId()).append(",");
                } else if (TYPE_2 == entity.getItemType()) {
                    typeIds.append(commonBean.getId()).append(",");
                }
            }
            if (classificationIds.length() > 0) {
                classificationIds = new StringBuilder(classificationIds.substring(0, classificationIds.length() - 1));
            }
            if (typeIds.length() > 0) {
                typeIds = new StringBuilder(typeIds.substring(0, typeIds.length() - 1));
            }
            keys[0] = classificationIds.toString();
            keys[1] = typeIds.toString();
        }
        return keys;
    }

}
